<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 新闻列表
//-------------------------

namespace app\admin\controller;
\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);


use app\admin\Controller;

use think\Db;
use think\Lang;
use think\Loader;
use think\File;
use think\Request;
use think\Exception;

class Import extends Controller
{
    use \app\admin\traits\controller\Controller;
	use \traits\controller\Jump;
		
    /**
     * 列表
     */
//  public function index()
//  {
//
//      if ($this->request->isPost()) {      	
//		
//      } else {       	
//			
//			$list = Loader::model("SecurityCode")->order("id desc")->paginate(20);
//
//			$this->view->assign("list",$list);
//			$this->view->assign("page",$list->render());
//			$this->view->assign("count",$list->total());
//			
//          return $this->view->fetch();
//      }
//  }
		
	public function index()
    {
        $model = $this->getModel('security_code');
		//dump($model);die;
        $map = array();
        // id
        if ($this->request->param('id')) $map['n.id'] = ['eq',   $this->request->param('id')  ];
        // 标题
        if ($this->request->param('title')) $map['n.title'] = ['eq',   $this->request->param('title')  ];
        // 查询字段
        $field = 'n.id,n.title,n.count,n.code';
        $listRows = 20;
        // 分页查询
        $list = $model->alias('n')->field($field)
			->where($map)
            ->order('  id desc')->paginate($listRows, false, ['query' => $this->request->get()]);

		
		//print_r($list);//die;
        // 模板赋值显示
        //dump($lsit);die;
        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
       // $this->view->assign('numPerPage', $listRows);

        return $this->view->fetch();
    }
		
		

    /**
     * 默认删除操作
     */
    public function delete()
    {
  		$id = $this->request->param('id');		
		
		if($id != ""){
			$idArr = explode(",", $id);
			if(is_array($idArr)){
				foreach($idArr as $v){
					Db::table('jx_security_code")->where(" id = ".$v)->delete();				
				}
			}else{
				Db::table('jx_security_code")->where(" id = ".$id)->delete();	
			}
			return ajax_return_adv("删除成功");			
  		}else{
  			return ajax_return_adv_error('请选择要删除的项');
  			//return ajax_return_adv('请输入要删除的选项');			
  		}
		
       
    }
	




	public 	function Import(){
		$id=input('post.');//二维数组
		
		$id = $id['id'];
			
//		if($this->request->isPost()){
			
			$type = $this->request->param("type");			
			
			
			if($type == "Export"){ //导出 export
				
	            $header = ['ID', '名称', '编码', '检验次数'];
			

				$data = Db::name("SecurityCode")->order("id desc")->select($id);
				
				//dump($data);exit;
				
	          //$data = Db::name("SecurityCode")->order("id desc")->limit(20)->select();
	            if ($error = \Excel::export($header, $data, "Excel导出", '2007')) {
	                throw new Exception($error);
	            }				
				
			}
			elseif($type == "Import"){	//导入  Import
				header("content-type:text/html;charset=utf-8");
				
				$file = $this->request->file('filename');
		        $path = ROOT_PATH.'public/tmp/uploads/';

		        $info = $file->move($path);
				
				$name = ROOT_PATH . 'public/tmp/uploads/' . $info->getSaveName();
				
				$name = str_replace("\\","/",$name);
				
				$header = ['B'=>'title', 'C'=>'code']; 
	            
				$insertF = insertFunc();
				
				
	            if ($count = \Excel::parse($name, $header, 10 , $insertF,"")) {

	                return '<script>parent.layer.alert("导入数据'.$count.'条")</script>';

	            }					
				
			}			
			
			
//		}else{
//			
		return $this->view->fetch();
//		}
	}

	/**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();
        $module = $this->request->module();
		
//		var_dump($this->request->isAjax());
//		exit;
		
        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
//			var_dump($data);
//			exit;
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
                        
            // 验证
            if (class_exists(Loader::parseClass($module, 'validate', $controller))) {
                $validate = Loader::validate($controller);
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }
            // 更新数据
            Db::startTrans();
            try {
              //  $data['addtime'] = time();
                Db::table('jx_security_code")->where(" id = ".$data['id'])->update($data);
                // 提交事务
                Db::commit();

                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();

                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $rId = $this->request->param('id');			
            if (!$id) {
                throw new Exception("缺少参数ID");
            }			
            //$vo = $this->getModel($controller)->find($rId);
			$vo =  Db::table('jx_security_code")->where(" id = ".$id)->find();	
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);
			
            return $this->view->fetch();
        }
    }










}